//--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; int i, j; int x, y; int world[10][10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15,16,17,18,19, 20,21,22,23,24,25,26,27,28,29, 30,31,32,33,34,35,36,37,38,39, 40,41,42,43,44,45,46,47,48,49, 50,51,52,53,54,55,56,57,58,59, 60,61,62,63,64,65,66,67,68,69, 70,71,72,73,74,75,76,77,78,79, 80,81,82,83,84,85,86,87,88,89, 90,91,92,93,94,95,96,97,98,99, }; int w (int coord) { return (coord + 10) % 10; } //--------------------------------------------------- Color Ramp Version 2012 TColor colorRamp(int part, int whole) { if (whole == 0) whole = 1; // prevent divide by zero part = part % whole; // keep part <= whole int pixelDistanceAlongEdges = (part * 1792) / whole; int red, green, blue; // Which edge of the color cube are we on? if (pixelDistanceAlongEdges < 256) { // from BLACK to BLUE red = 0; green = 0; blue = pixelDistanceAlongEdges; } else if (pixelDistanceAlongEdges < 512) { // from BLUE to CYAN red = 0; green = pixelDistanceAlongEdges - 256; blue = 255; } else if (pixelDistanceAlongEdges < 768) { // from CYAN to GREEN red = 0; green = 255; blue = 255 - (pixelDistanceAlongEdges - 512); } else if (pixelDistanceAlongEdges < 1024) { // from GREEN to YELLOW red = (pixelDistanceAlongEdges - 768); green = 255; blue = 0; } else if (pixelDistanceAlongEdges < 1280) { // from YELLOW to RED red = 255; green= 255-(pixelDistanceAlongEdges - 1024); blue = 0; } else if (pixelDistanceAlongEdges < 1536) { // from RED to MAGENTA red = 255; green= 0; blue = pixelDistanceAlongEdges - 1280; } else { // from MAGENTA to WHITE red = 255; green = pixelDistanceAlongEdges - 1537; blue = 255; } return static_cast(RGB(red, green, blue)); } void showRectangle (void) { Form1->PaintBox1->Refresh(); for (i = 0; i < 10; i++) { for (j = 0; j < 10; j++) { Form1->PaintBox1->Canvas->Brush->Color = colorRamp(world[i][j], 100); Form1->PaintBox1->Canvas->Pen->Color = colorRamp(100 - world[i][j], 100); Form1->PaintBox1->Canvas->Rectangle(i * 50, j * 50, i * 50 + 50, j * 50 + 50); } } } void draw (int x, int y) { if (y % 2 == 0) { // even row Form1->PaintBox1->Canvas->Ellipse(x * 50, y * 50, x * 50 + 50, y * 50 + 50); } else { // odd row Form1->PaintBox1->Canvas->Ellipse(x * 50 + 25, y * 50, x * 50 + 50 + 25, y * 50 + 50); } } void showCircle (void) { Form1->Refresh(); for (x = 0; x < 10; x++) { // column for (y = 0; y < 10; y++) { // row Form1->PaintBox1->Canvas->Brush->Color = colorRamp(world[x][y], 100); Form1->PaintBox1->Canvas->Pen->Color = colorRamp(100 - world[x][y], 100); draw(x, y); } } } void showHexNeighbors (int x, int y) { Form1->PaintBox1->Canvas->Brush->Color = clGray; Form1->PaintBox1->Canvas->Pen->Color = clGray; if (y % 2 == 0) { // even row draw(w(x-1), w(y-1)); draw(x, w(y-1)); draw(w(x-1),y); draw(w(x+1), y); draw(w(x-1), w(y+1)); draw(x, w(y+1)); } else { // odd row draw(x, w(y-1)); draw(w(x+1), w(y-1)); draw(w(x-1), y); draw(w(x+1), y); draw(x, w(y+1)); draw(w(x+1), w(y+1)); } } //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { Form1->PaintBox1->Height = 500; Form1->PaintBox1->Width = 525; } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonShowRectangleClick(TObject *Sender) { showRectangle(); } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonShowCircleClick(TObject *Sender) { showCircle(); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { showHexNeighbors(5, 5); } //--------------------------------------------------------------------------- void __fastcall TForm1::PaintBox1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { showCircle(); x = (X - 25)/ 50; y = Y / 50; Form1->EditHexColumn->Text = x; Form1->EditHexRow->Text = y; showHexNeighbors(x, y); x = X / 50; y = Y / 50; Form1->EditSquareColumn->Text = x; Form1->EditSquareRow->Text = y; } //---------------------------------------------------------------------------